<template>
  <view class="search-box">
    <uni-search-bar @input="input" :radius="100" placeholder="请输入搜索内容" cancelButton="none" :focus="true"></uni-search-bar>
    <view class="sugg-list">
      <view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
        <view class="goods-name">
          {{item.goods_name}}
        </view>
        <uni-icons type="arrowright" size="16"></uni-icons>
      </view>
    </view>
    <view class="history-box" v-if="!kw">
      <view class="history-title">
        <text>搜素历史</text>
        <uni-icons type="trash" size="17" @click="deleHistory"></uni-icons>
      </view>
      <view class="history-list">
        <uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoods(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer:null,
        kw:"",
        searchResults:[],
        historyList:[],
      }
    },
    onLoad(){
      this.historyList=JSON.parse(uni.getStorageSync('kw')||'[]')
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer=setTimeout(()=>{
          this.kw=e.trim();
          this.getSearchList();
          this.saveSearchHistory();
        },500)
      },
      saveSearchHistory(){
        const set=new Set(this.historyList)
        set.delete(this.kw)
        set.add(this.kw)
        this.historyList=Array.from(set)
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      gotoDetail(goods_id){
        uni.navigateTo({
          url:`../goods_detail/goods_detail?goods_id=${goods_id}`
        })
      },
      getSearchList(){
        if(this.kw===''){
          this.searchResults=[]
          return;
        }
        this.getList('/api/public/v1/goods/qsearch','searchResults',{ query: this.kw })
      },
      deleHistory(){
        this.historyList=[]
        uni.setStorageSync('KW','[]')
      },
      gotoGoods(kw){
        uni.navigateTo({
          url:'../goods_list/goods_list?query='+kw
        })
      }
    },
    computed:{
      histories(){
          return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss">
  .search-box{
  .sugg-list{
    padding: 0 5px;
    .sugg-item{
      padding: 13px 0;
      font-size: 12px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      border-bottom: 1px solid #efefef;
      .goods-name{
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
    }
    .history-box{
      padding: 0 10px;
      .history-title{
        display: flex;
        justify-content: space-between;
        font-size: 13px;
        border-bottom: 1px solid #efefef;
        margin-bottom: 10px;
      }
      .history-list{
        .uni-tag{
          margin-right: 10rpx;
        }
      }
    }
  }
</style>
